package net.qhzw.vip.qiyu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.DtsCarRental;
import net.qhzw.vip.qiyu.entity.SysUser;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.model.SysCarRentalQuery;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.DtsCarRentalService;
import net.qhzw.vip.qiyu.service.SysUserService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("sys/carRental")
@Api(tags = "后台租车申请管理")
public class SysCarRentalController {


    @Autowired
    private DtsCarRentalService dtsCarRentalService;


    @Autowired
    private SysUserService sysUserService;


    @ApiOperation(value = "后台管理员查询租车申请")
    @GetMapping
    public JsonResult findByPage(Page<DtsCarRental> page, SysCarRentalQuery query) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        if (query.getId() == null) {
            Long userId = SecurityUtils.getUserId();
            SysUser user = sysUserService.getById(userId);
            if (user == null || user.getCommunityId() == null) {
                return JsonResult.ok(new HashMap<>());
            }
            if (user.getCommunityId() != 1L) {
                query.setAdminCommunityId(user.getCommunityId());
            }
        }
        //query.setStatus((byte)2);
        Map<String, Object> result = dtsCarRentalService.sysFindByPage(page, shopId, query);
        return JsonResult.ok(result);
    }

    @ApiOperation(value = "根据租车id获取合同")
    @GetMapping("{id}")
    public JsonResult createContract(@PathVariable Long id) {
        DtsCarRental dtsCarRental = dtsCarRentalService.getById(id);
        return JsonResult.ok(dtsCarRental);
    }

    @ApiOperation(value = "后台管理员审核租车申请")
    @PostMapping("audit")
    public JsonResult audit(@RequestBody Map<String, String> param) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        if (shopId == 1) {
            throw new ApiException("您是超级管理员, 不能审核该订单!");
        }
        Long id = Long.valueOf(param.get("id"));
        //审核状态
        Byte status = Byte.valueOf(param.get("status"));
        //审核意见
        String auditOpinion = param.get("auditOpinion");
        boolean isOK = dtsCarRentalService.audit(id, status, auditOpinion);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "关闭合同")
    @PostMapping("closeContract/{id}")
    public JsonResult closeContract(@PathVariable Long id) {
        boolean isOK = dtsCarRentalService.closeContract(id);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    //exportContract
    @ApiOperation(value = "导出合同")
    @GetMapping("exportContract/{id}")
    public void exportContract(@PathVariable Long id, HttpServletResponse response) throws IOException {
        dtsCarRentalService.exportContract(id, response);
    }

    @ApiOperation(value = "导出今日租车明细")
    @GetMapping("export")
    public void export(HttpServletResponse response, String contractNo, String idCard, String mobile, String realName, Long communityId) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        dtsCarRentalService.export(response, shopId, contractNo, idCard, mobile, realName, communityId);
    }


    /**
     * 报停 暂停租车业务
     *
     * @return
     */
    @PostMapping("stopRep/{id}")
    public JsonResult stopRep(@PathVariable Long id) {
        boolean isOK = dtsCarRentalService.stopRep(id);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    /**
     * 恢复租车状态
     *
     * @return
     */
    @PostMapping("recovery")
    public JsonResult recovery(@RequestBody Map<String, Long> param, HttpServletRequest request) {
        boolean isOK = dtsCarRentalService.recovery(param, request);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

}
